/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Nhanvien;
import util.HibernateUtil;

/**
 *
 * @author sangnguyen   
 */
public class NhanVienDAO {
    public static boolean InsertNhanvien(Nhanvien obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            session.save(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();
        }
        return true;
    }
    public static boolean UpdateNhanvien(Nhanvien obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();        
        try{
            session.update(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();                            
        }
        
        return true;
    }
     public static boolean DeleteNhanvien(Nhanvien obj){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try{
            obj.setTinhtrang(0);
            session.update(obj);
            tx.commit();
        }
        catch(HibernateException ex){
            tx.rollback();
            System.err.println(ex);
            return false;
        }
        finally{
            session.close();        
        }
        return true;
    }
     public static List<Nhanvien> SelectAllNhanvien(){
         List<Nhanvien> lsNhanvien = null;
         Session session = HibernateUtil.getSessionFactory().openSession();   
         try{
            Query query = session.createQuery("from Nhanvien nv where nv.tinhtrang=:tinhTrang order by nv.id asc");
            query.setInteger("tinhTrang", 1);
            lsNhanvien = query.list();         
         }
         catch(HibernateException ex){
             System.err.println(ex);
         }
         finally{
            session.close();        
         }
         return lsNhanvien;
     }
     public static Nhanvien GetNhanvienID(int id){
         Nhanvien obj = new Nhanvien();
         Session session = HibernateUtil.getSessionFactory().openSession();   
         try{
             obj = (Nhanvien)session.get(Nhanvien.class, id);         
         }
         catch(HibernateException ex){
             System.err.println(ex);
         }
         finally{
             session.close();        
         }         
         return obj;
     }
     public static Nhanvien GetNhanvienUsername(String username){
         Nhanvien obj = new Nhanvien();
         Session session = HibernateUtil.getSessionFactory().openSession();   
         try{
             String hql = "Select nv from Nhanvien nv where username=:Username";
             Query query = session.createQuery(hql);         
             query.setString("Username", username);             
             obj = (Nhanvien)query.uniqueResult();
         }
         catch(HibernateException ex){
             System.err.println(ex);
         }
         finally{
             session.close();        
         }         
         return obj;
     }
     public static Nhanvien ValidateNhanvien(String username, String password){
         Nhanvien obj = null;
         Session session = HibernateUtil.getSessionFactory().openSession();   
         try{
             String hql = "Select nv from Nhanvien nv where nv.username=:Username and nv.password=:Password and nv.tinhtrang=:tinhTrang";
             Query query = session.createQuery(hql);         
             query.setString("Username", username);             
             query.setString("Password", password);             
             query.setInteger("tinhTrang", 1);             
             obj = (Nhanvien)query.uniqueResult();
         }
         catch(HibernateException ex){
             System.err.println(ex);
         }
         finally{
             session.close();        
         }         
         return obj;
     }
}
